<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="消防设备id" prop="deviceId">
      <el-input v-model="dataForm.deviceId" placeholder="消防设备id"></el-input>
    </el-form-item>
    <el-form-item label="维修员id" prop="repairmanId">
      <el-input v-model="dataForm.repairmanId" placeholder="维修员id"></el-input>
    </el-form-item>
    <el-form-item label="维修日期" prop="repairDate">
      <el-input v-model="dataForm.repairDate" placeholder="维修日期"></el-input>
    </el-form-item>
    <el-form-item label="维修类型，如定期保养，故障维修，更换配件" prop="repairType">
      <el-input v-model="dataForm.repairType" placeholder="维修类型，如定期保养，故障维修，更换配件"></el-input>
    </el-form-item>
    <el-form-item label="故障描述" prop="faultDescription">
      <el-input v-model="dataForm.faultDescription" placeholder="故障描述"></el-input>
    </el-form-item>
    <el-form-item label="维修具体操作" prop="operation">
      <el-input v-model="dataForm.operation" placeholder="维修具体操作"></el-input>
    </el-form-item>
    <el-form-item label="维修结果" prop="result">
      <el-input v-model="dataForm.result" placeholder="维修结果"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          deviceId: '',
          repairmanId: '',
          repairDate: '',
          repairType: '',
          faultDescription: '',
          operation: '',
          result: ''
        },
        dataRule: {
          deviceId: [
            { required: true, message: '消防设备id不能为空', trigger: 'blur' }
          ],
          repairmanId: [
            { required: true, message: '维修员id不能为空', trigger: 'blur' }
          ],
          repairDate: [
            { required: true, message: '维修日期不能为空', trigger: 'blur' }
          ],
          repairType: [
            { required: true, message: '维修类型，如定期保养，故障维修，更换配件不能为空', trigger: 'blur' }
          ],
          faultDescription: [
            { required: true, message: '故障描述不能为空', trigger: 'blur' }
          ],
          operation: [
            { required: true, message: '维修具体操作不能为空', trigger: 'blur' }
          ],
          result: [
            { required: true, message: '维修结果不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/repair/repairrecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.deviceId = data.repairRecord.deviceId
                this.dataForm.repairmanId = data.repairRecord.repairmanId
                this.dataForm.repairDate = data.repairRecord.repairDate
                this.dataForm.repairType = data.repairRecord.repairType
                this.dataForm.faultDescription = data.repairRecord.faultDescription
                this.dataForm.operation = data.repairRecord.operation
                this.dataForm.result = data.repairRecord.result
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/repair/repairrecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'deviceId': this.dataForm.deviceId,
                'repairmanId': this.dataForm.repairmanId,
                'repairDate': this.dataForm.repairDate,
                'repairType': this.dataForm.repairType,
                'faultDescription': this.dataForm.faultDescription,
                'operation': this.dataForm.operation,
                'result': this.dataForm.result
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
